package com.ms.education.course.controller;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.ms.education.R;
import com.ms.education.course.entity.Chapter;
import com.ms.education.course.entity.vo.ChapterSortVo;
import com.ms.education.course.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 课程章节 前端控制器
 * </p>
 *
 * @author ms
 * @since 2023-01-17
 */
@RestController
@RequestMapping("/course/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @PostMapping("saveOrUpdateChapter")
    public R saveOrUpdate(@RequestBody Chapter chapter) {
        chapterService.saveOrUpdate(chapter);
        return R.ok();
    }

    @GetMapping("getChapterById/{id}")
    public R getChapterById(@PathVariable("id") Integer id) {
        Chapter chapter = chapterService.getById(id);
        return R.ok().data("chapter", chapter);
    }

    @DeleteMapping("deleteChapter/{id}")
    public R deleteChapter(@PathVariable("id")Integer id) {
        chapterService.removeById(id);
        return R.ok();
    }

    @GetMapping("getChaptersByCourseId/{courseId}")
    public R getChaptersByCourseId(@PathVariable("courseId") Integer courseId) {
        List<Chapter> chapters = chapterService.list(courseId);
        return R.ok().data("chapters", chapters);
    }

    @PostMapping("updateChapterAndVideoSort")
    public R updateChapterAndVideoSort(@RequestBody List<ChapterSortVo> chapterSortVos){
        chapterService.updateChapterAndVideoSort(chapterSortVos);
        return R.ok();
    }

}
